// 产品创建页面工具函数

import { DEFAULT_TEXTS, BUSINESS_TYPES, BUSINESS_ENTRIES, ORIGIN_TYPES } from './constants.js';

/**
 * 根据用户类型和入口判断商品来源类型
 * @param {number} businessType - 业务类型
 * @param {string} businessEntry - 入口标识
 * @returns {number} originType
 */
export function getOriginType(businessType, businessEntry) {
  if (businessType === BUSINESS_TYPES.SECOND) {
    // 二类用户根据入口判断
    if (businessEntry === BUSINESS_ENTRIES.DEST_RECEIVE || businessEntry === BUSINESS_ENTRIES.GROUP_TRAVEL) {
      return ORIGIN_TYPES.CALL; // 从目的地接待业务与组团业务进入
    } else if (businessEntry === BUSINESS_ENTRIES.COMPREHENSIVE_RESEARCH) {
      return ORIGIN_TYPES.SELF_BUILT; // 从综合研学进入
    }
  }
  
  // 一类用户或其他情况默认为自建
  return ORIGIN_TYPES.SELF_BUILT;
}

/**
 * 初始化固定提示内容
 * @param {Object} productStore - 产品存储对象
 */
export function initializeFixedPrompts(productStore) {
  // 初始化保障提示
  if (!productStore.subPageData.guaranteePrompt) {
    productStore.subPageData.guaranteePrompt = [{
      title: '保障提示',
      alt: DEFAULT_TEXTS.GUARANTEE_PROMPT
    }];
  }

  // 初始化风险提示
  if (!productStore.subPageData.riskPrompt) {
    productStore.subPageData.riskPrompt = DEFAULT_TEXTS.RISK_PROMPT;
  }
}

/**
 * 处理费用数组数据，去除指定字段并添加order
 * @param {Array} items - 费用项目数组
 * @param {Array} fieldsToRemove - 需要去除的字段
 * @returns {Array} 处理后的数组
 */
export function processExpenseItems(items, fieldsToRemove = ['isOriginal', 'id']) {
  if (!items || items.length === 0) return [];
  
  return items.map((item, index) => {
    const processedItem = { ...item };
    fieldsToRemove.forEach(field => {
      delete processedItem[field];
    });
    processedItem.order = index + 1;
    return processedItem;
  });
}

/**
 * 获取描述标签
 * @param {string} type - 类型
 * @returns {string} 标签文本
 */
export function getDescriptionLabel(type) {
  const labels = {
    '集散': '集散说明',
    '交通': '交通说明',
    '餐饮': '用餐说明',
    '活动': '活动描述',
    '住宿': '住宿说明',
    '用餐': '用餐说明'
  };
  return labels[type] || '详细描述';
}

/**
 * 检查表单是否有内容
 * @param {Object} form - 表单数据
 * @param {Object} productStore - 产品存储
 * @returns {boolean} 是否有内容
 */
export function hasFormContent(form, productStore) {
  // 检查基本信息
  if (form.name && form.name.trim() !== '') return true;
  if (form.title && form.title.trim() !== '') return true;
  if (form.images && form.images.length > 0) return true;
  
  // 检查子页面数据
  if (productStore && productStore.subPageData) {
    const subData = productStore.subPageData;
    if (subData.productFeatures && subData.productFeatures.length > 0) return true;
    if (subData.specificItinerary && subData.specificItinerary.length > 0) return true;
    if (subData.expensesIncluded && subData.expensesIncluded.length > 0) return true;
    if (subData.expensesExcluded && subData.expensesExcluded.length > 0) return true;
    if (subData.otherExpenses && subData.otherExpenses.length > 0) return true;
  }
  
  return false;
}

/**
 * 格式化图片数据
 * @param {Array} images - 图片数组
 * @returns {Array} 格式化后的图片数组
 */
export function formatImages(images) {
  if (!images || images.length === 0) return [];
  
  return images.map((img, index) => ({
    url: img.url || '',
    alt: img.alt || '',
    order: img.order || index + 1,
    width: img.width || 0,
    height: img.height || 0
  }));
}

/**
 * 生成产品预览数据
 * @param {Object} form - 表单数据
 * @param {Object} productStore - 产品存储
 * @param {Array} ageRange - 年龄范围
 * @returns {Object} 预览数据
 */
export function generatePreviewData(form, productStore, ageRange) {
  return {
    // 基本信息
    name: form.name || '',
    title: form.title || '',
    images: form.images || [],
    
    // 子页面数据
    productFeatures: productStore?.subPageData?.productFeatures || [],
    specificItinerary: productStore?.subPageData?.specificItinerary || [],
    expensesIncluded: productStore?.subPageData?.expensesIncluded || [],
    expensesExcluded: productStore?.subPageData?.expensesExcluded || [],
    otherExpenses: productStore?.subPageData?.otherExpenses || [],
    
    // 预定须知
    bookingInfo: productStore?.subPageData?.bookingInfo || DEFAULT_TEXTS.DEFAULT_BOOKING_INFO,
    
    // 固定提示
    guaranteePrompt: DEFAULT_TEXTS.GUARANTEE_PROMPT,
    riskPrompt: DEFAULT_TEXTS.RISK_PROMPT,
    
    // 年龄范围
    ageRange: ageRange || [6, 11]
  };
}






